﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using automobili_data;
using automobili_data.interfaces;
using System.Net.Mail;
using System.Net;
using System.IO;

namespace automobili_gui.Ulogovan_korisnik
{
    public partial class Oglasavanje : System.Web.UI.Page
    {
        User k;
        Privilegije p;
        ArrayList mod, mar, gor, kar;
        String mark = null, mode = null, goriv = null, karoserij = null;
        double cena;
        int provera = 0;
        int godina;
        protected void Page_Load(object sender, EventArgs e)
        {

            k = SessionUtils.getCurrentUser();
           p = SessionUtils.getCurrentPrivileges();
         //  p = AutomobiliDataManager.instance.dohvatiPrivilegijeP(k.UserId);

                if (!Page.IsPostBack)
                {
                    if (p.Oglasavanje_k == 1)
                    {
                        dohvatiModele();
                        dohvatiMarke();
                        dohvatiGorivo();
                        dohvatiKaroseriju();
                    }
                }
            }
        
        private void dohvatiModele()
        {
            mod = AutomobiliDataManager.instance.dohvatiModele();
            mod.Insert(0, "");
            modelLista.DataSource = mod;
            modelLista.DataBind();

        }

        private void dohvatiMarke()
        {
            mar = AutomobiliDataManager.instance.dohvatiMarke();
            mar.Insert(0, "");
            markaLista.DataSource = mar;
            markaLista.DataBind();
        }

        private void dohvatiGorivo()
        {
            gor = AutomobiliDataManager.instance.dohvatiGorivo();
            gor.Insert(0, "");
            gorivoLista.DataSource = gor;
            gorivoLista.DataBind();

        }

        private void dohvatiKaroseriju()
        {
            kar = AutomobiliDataManager.instance.dohvatiKaroseriju();
            kar.Insert(0, "");
            karoserijaLista.DataSource = kar;
            karoserijaLista.DataBind();

        }

        protected void unesi(object sender, EventArgs e)
        {
            if (godinaUnos.Text != "")
            {
                try
                {
                    godina = Int32.Parse(godinaUnos.Text);
                    if (godina < 1900)
                    {
                        godP.Text = "Godina proizvodnje ne sme biti manja od 1900!";
                        provera = 1;
                    }
                }
                catch
                {
                    godP.Text = "Greska, morate uneti broj!";
                    provera = 1;
                }

            }
            else {
                godP.Text = "Greska, morate uneti godinu proizvodnje!";
            }


            if (cenaUnos.Text != "")
            {
                try
                {
                    cena = Double.Parse(cenaUnos.Text);
                    if (cena < 0)
                    {
                        cen.Text = "Cena ne sme biti negativan broj!";
                        provera = 1;
                    }
                }
                catch
                {
                    cen.Text = "Greska, morate uneti broj!";
                    provera = 1;
                }
            }
            else {
                cen.Text = "Greska, morate uneti cenu!";
            }
            if (markaLista.SelectedValue != "") { mark = markaLista.SelectedItem.Value; }
            else {
                marka.Text = "Greska, morate uneti marku!";
                provera=1;
            }
            if (modelLista.SelectedValue != "") { mode = modelLista.SelectedItem.Value; }
            else { 
                model.Text = "Greska, morate uneti model!"; 
                provera=1;
            }
            if (gorivoLista.SelectedValue != "") {goriv = gorivoLista.SelectedItem.Value;}
            else {
                gorivo.Text="Greska, morate uneti pogonsko gorivo!";
                provera=1;
            }
            if (karoserijaLista.SelectedValue != "") { karoserij = karoserijaLista.SelectedItem.Value; }
            else {
                karoserija.Text = "Greska, morate uneti oblik karoserije!";
                provera = 1;
            }

            if (provera == 0) {
                
                AutomobiliDataManager.instance.unesiOglas(mark, mode, karoserij, goriv, cena, godina);
                AutomobiliDataManager.instance.kreirajOglas(k.UserId, 1);
                IList<Oglas> ogl = AutomobiliDataManager.instance.podaciZaMail(k.UserId);
                int id = ogl[0].IdOglasa;
                DateTime datum = ogl[0].Datum;
                this.Send(id, datum);
                               
            }
        }//kraj f-je

        private void SendEMail (MailMessage mailMessage)
        {
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("automobil.portal@gmail.com", "iep_2013");
            client.Host = "smtp.gmail.com";
          
            try
            {
                client.Send(mailMessage);
            }
            catch (SmtpException)
            {
                throw;
            }
        }

        public void Send (int id, DateTime datum)
        {
            MailMessage Message = new MailMessage();
            MailAddress Email = new MailAddress("jeca_dk@yahoo.com");
            Message.To.Add(Email);
            //Message.From = new MailAddress(k.UserName);
            Message.From = new MailAddress("automobil.portal@gmail.com");
            Message.Subject = "Proba";
            Message.Body = "Id vaseg oglasa je: "+id+". Kreiran je: "+datum;
            SendEMail(Message);
        }
    }
}